package jp.gr.java_conf.ionexux.feztimer;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;

public class BookFragment extends Fragment implements OnClickListener {

	private final String TAG = "FragmentCrystalFragment";

	private Handler mHandler = new Handler();
	private MediaPlayer mPlayer;
	private Timer mTimerBookCounterView;
	private Timer mTimerBookBackGround;
	private View mProgressBar;
	private View mButtonStart;
	private View mButtonStop;
	private Context mContext;
	private int ringTone;
	private int progressLapTime;
	private int finishBookTime;
	private int cycleRingToneLapTime;
	private int currentTime;
	private boolean mBookFlag = false;

	@Override
	public void onAttach(Activity act) {
		super.onAttach(act);
		Log.d(TAG, "onAttach");
		mContext = getActivity();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView");

		// レイアウトを生成
		View v = inflater.inflate(R.layout.book, container, false);
		mProgressBar = v.findViewById(R.id.bookProgressBar1);
		mButtonStart = v.findViewById(R.id.bookStartButton);
		mButtonStop = v.findViewById(R.id.bookStopButton);

		// clickListenerを設定
		((Button) mButtonStart).setOnClickListener(this);
		((Button) mButtonStop).setOnClickListener(this);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		super.onActivityCreated(bundle);
		Log.d(TAG, "onActivityCreated");
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "onStart");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.d(TAG, "onResume");
		progressLapTime = 1000;
		finishBookTime = 60 * 27 * 1000;
		cycleRingToneLapTime = 10 * 1000;
		((ProgressBar) mProgressBar).setMax(finishBookTime);
		ringTone = R.raw.powercounter;
	}

	@Override
	public void onPause() {
		super.onPause();
		Log.d(TAG, "onPause");

	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "onStop");
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		Log.d(TAG, "onDestroyView");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
	}

	@Override
	public void onDetach() {
		super.onDetach();
		Log.d(TAG, "onDetach");
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.bookStartButton:
			startCounter();
			break;
		case R.id.bookStopButton:
			stopCounter();
			break;

		default:
			break;
		}

	}

	private void startCounter() {
		if (mBookFlag)
			stopCounter();
		playSound(ringTone);
		mBookFlag = true;
		mTimerBookCounterView = new Timer();
		mTimerBookCounterView.schedule(new TimerTask() {
			@Override
			public void run() {
				currentTime = currentTime + progressLapTime;
				mHandler.post(new Runnable() {
					public void run() {
						((ProgressBar) mProgressBar).setProgress(currentTime);
					}
				});
			}
		}, 0, progressLapTime);

		mTimerBookBackGround = new Timer();
		mTimerBookBackGround.schedule(new TimerTask() {
			@Override
			public void run() {
				playSound(ringTone);
			}
		}, finishBookTime, cycleRingToneLapTime);

	}

	private void stopCounter() {
		if (mBookFlag) {
			cancelCountTimer();
			((ProgressBar) mProgressBar).setProgress(0);
		}
	}

	private void cancelCountTimer() {
		mBookFlag = false;

		mTimerBookBackGround.cancel();
		mTimerBookBackGround = null;

		mTimerBookCounterView.cancel();
		mTimerBookCounterView = null;
	}

	public void playSound(int ringTone) {
		stopSound();
		mPlayer = MediaPlayer.create(mContext, ringTone);
		mPlayer.seekTo(0);
		mPlayer.start();
		mPlayer.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
				stopSound();
			}
		});

	}

	public void stopSound() {
		if (mPlayer == null)
			return;
		mPlayer.stop();
		mPlayer.setOnCompletionListener(null);
		mPlayer.release();
		mPlayer = null;
	}
}
